package com.lowca.news.web.jsonbean;

import java.util.Date;
import java.util.Set;

import com.lowca.news.entity.Role;
import com.lowca.news.entity.User;
import com.lowca.news.util.Collections3;

import static com.lowca.news.constant.Constants.RoleMeta;

public class UserWarpperBean {

	private Long id;
	private String username;
	private String email;
	private String roleName;
	private Date registerTime;
	private Date lastLoginTime;
	private Boolean enabled;

	public UserWarpperBean(User user) {
		this.id = user.getId();
		this.username = user.getUsername();
		this.email = user.getEmail();
		Set<Role> roles = user.getRoles();
		String rolesStr = Collections3.extractToString(roles, "name", ",");
		StringBuffer sb = new StringBuffer();
		for (RoleMeta roleMeta : RoleMeta.values()) {
			if (rolesStr.contains(roleMeta.getName())) {
				sb.append("," + roleMeta.getDisplayName());
			}
		}
		if (sb.length() > 0) {
			sb.delete(0, 1);
		}
		this.roleName = sb.toString();
		this.registerTime = user.getRegisterTime();
		this.lastLoginTime = user.getLastLoginTime();
		this.enabled = user.getEnabled();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public Date getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(Date registerTime) {
		this.registerTime = registerTime;
	}

	public Date getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

}
